/******************************************************************************
 * $Id: popup.c,v 1.18 2004/09/22 01:14:23 jimb Exp $
 * @file
 * @author  Jim Brooks http://www.jimbrooks.org
 * @brief   Functions for popup windows.
 ******************************************************************************/

#include "common.h"
#include "popup.h"

#include "widgets.h"

/******************************************************************************
 * Display a popup message with a single OK button.
 * @param   pMsg
 *          Message to display.
 * @param   pTitle
 *          The title of the popup widget.
 * @param   whichFont
 *          FONT_MONOSPACED or FONT_PROPORTIONAL
 ******************************************************************************/
void
Popup( char* pMsg,
       char* pTitle,
       int   whichFont )
{
   GtkWidget*	pWidgetLabel;
   GtkWidget*	pWidgetButton;
   GtkWidget*	pWidgetDialog;

  /*
   * Create a dialog window.
   */
   pWidgetDialog = gtk_dialog_new();

  /*
   * Trap the window close signal to release the grab.
   */
   gtk_signal_connect( GTK_OBJECT(pWidgetDialog),
                       "destroy",
                       GTK_SIGNAL_FUNC(PopupClosing),
                       &pWidgetDialog );
  /*
   * Title the popup.
   */
   gtk_window_set_title( GTK_WINDOW(pWidgetDialog),
                         _(pTitle ));

  /*
   * Create a small border.
   */
   gtk_container_border_width( GTK_CONTAINER(pWidgetDialog), 5 );

  /*
   * Create a label widget for the message.
   */
   pWidgetLabel = gtk_label_new( _(pMsg ));

  /*
   * Put padding around the label.
   */
   gtk_misc_set_padding( GTK_MISC(pWidgetLabel),
                         10,
                         10 );

  /*
   * Set the justification of the label.
   */
   gtk_label_set_justify( GTK_LABEL(pWidgetLabel),
                          GTK_JUSTIFY_LEFT );

  /*
   * Use monospaced font if specified.
   * GTK defaults to proportional fonts.
   */
   if ( whichFont == FONT_MONOSPACED )
     ChangeStyleFont( pWidgetLabel, "fixed" );

  /*
   * Add the label to the dialog.
   */
   gtk_box_pack_start( GTK_BOX( GTK_DIALOG(pWidgetDialog)->vbox),
                       pWidgetLabel,
                       TRUE,
                       TRUE,
                       0 );
  /*
   * Show the label.
   */
   gtk_widget_show( pWidgetLabel );

  /*
   * Create the OK button.
   */
   pWidgetButton = gtk_button_new_with_label(_( "OK" ));

  /*
   * Prepare for closing the popup when OK is pressed.
   */
   gtk_signal_connect( GTK_OBJECT(pWidgetButton),
                       "clicked",
                       GTK_SIGNAL_FUNC(PopupClose),
                       pWidgetDialog );

  /*
   * Let OK be the default button.
   */
   GTK_WIDGET_SET_FLAGS( pWidgetButton,
                         GTK_CAN_DEFAULT );

  /*
   * Add the button to the dialog.
   */
   gtk_box_pack_start( GTK_BOX( GTK_DIALOG(pWidgetDialog)->action_area),
                       pWidgetButton,
                       TRUE,
                       TRUE,
                       0 );
  /*
   * Make the OK button the default.
   */
   gtk_widget_grab_default( pWidgetButton );

  /*
   * Show the button.
   */
   gtk_widget_show( pWidgetButton );

  /*
   * Show the dialog.
   */
   PositionShowDialogAtMiddle( pWidgetDialog );

  /*
   * Grab the dialog.
   */
   gtk_grab_add( pWidgetDialog );

   return;
}

/******************************************************************************
 * Callback that closes the popup window.
 ******************************************************************************/
void
PopupClose( GtkWidget* pWidget,
            gpointer   pData )
{
  /*
   * Close the popup window.
   */
   gtk_widget_destroy( GTK_WIDGET(pData) );
}

/******************************************************************************
 * Callback for when the popup window is being closed.
 ******************************************************************************/
void
PopupClosing( GtkWidget* pWidget,
              gpointer   pData )
{
  /*
   * Release the grab which made the dialog window modal.
   */
   gtk_grab_remove( GTK_WIDGET(pWidget) );
}


// Copy and Paste from leafpad editor with little modify

GtkWidget *
CreateDialogMessageQuestion(GtkWidget *window, gchar *message, ...)
{
	va_list ap;
	GtkWidget *dialog;
	gchar *str;
	
	va_start(ap, message);
		str = g_strdup_vprintf(message, ap);
	va_end(ap);
	
	dialog = gtk_message_dialog_new(GTK_WINDOW(window),
		GTK_DIALOG_DESTROY_WITH_PARENT,
		GTK_MESSAGE_QUESTION,
		GTK_BUTTONS_NONE,
		str);
	gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
	gtk_dialog_add_buttons(GTK_DIALOG(dialog),
		GTK_STOCK_NO, GTK_RESPONSE_NO,
		GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
		GTK_STOCK_YES, GTK_RESPONSE_YES,
		NULL);
	gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_YES);
	g_free(str);
	
	return dialog;
}
/*
 * copy and paste from leafpad with little modify
 */
/* GTK_MESSAGE_INFO, GTK_MESSAGE_WARNING, GTK_MESSAGE_ERROR */
void RunDialogMessage(GtkWidget *window,
	GtkMessageType type,
	gchar *message, ...)
{
	va_list ap;
	GtkWidget *dialog;
	gchar *str;
	
	va_start(ap, message);
		str = g_strdup_vprintf(message, ap);
	va_end(ap);
	
	dialog = gtk_message_dialog_new(GTK_WINDOW(window),
		GTK_DIALOG_DESTROY_WITH_PARENT,
		type,
		GTK_BUTTONS_NONE,
		str);
	gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
	gtk_dialog_add_buttons(GTK_DIALOG(dialog),
		GTK_STOCK_OK, GTK_RESPONSE_CANCEL, NULL);
	gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);
	g_free(str);
	
	gtk_dialog_run(GTK_DIALOG(dialog));
	gtk_widget_destroy(dialog);
}


gint
RunDialogMessageQuestion(GtkWidget *window, gchar *message, ...)
{
	va_list ap;
	GtkWidget *dialog;
	gchar *str;
	gint res;
	
	va_start(ap, message);
		str = g_strdup_vprintf(message, ap);
	va_end(ap);
	
	dialog = CreateDialogMessageQuestion(window, str);
	g_free(str);
	
	res = gtk_dialog_run(GTK_DIALOG(dialog));
	gtk_widget_destroy(dialog);
	
	return res;
}
